//
//  ST_KnighthoodLevelCell.swift
//  btvideo
//
//  Created by Jason on 2021/8/30.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_KnighthoodLevelCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        contentView.backgroundColor = .random
        
        [icon_back_image_view,icon_image_view,level_title_label].forEach({ contentView.addSubview($0) })
        
        icon_back_image_view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalTo(62)
        }
        
        icon_image_view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(6)
            make.width.height.equalTo(50)
        }
        
        
        
        level_title_label.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(62+10)
            make.height.equalTo(20)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var icon_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        return v
    }()
    
//    fileprivate lazy var icon_back_image_view:UIImageView = {
//        let v = UIImageView(image: ST_ImageUtils.imageName("贵族特权_light"))
//        v.alpha = 0
//        return v
//    }()
    
    fileprivate lazy var icon_back_image_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "receiveGiftBack", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.loopAnimation = true
        
        control.alpha = 0
        
        return control
    }()
    
    
    fileprivate lazy var level_title_label:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.font = .systemFont(ofSize: 14)
        return l
    }()
    
    weak var model:ST_KnighthoodLevelModel?{
        didSet{
            
            guard let m = model,m.gapCell == false else {
                contentView.alpha = 0
                return
            }
            
            contentView.alpha = 1
            
            icon_image_view.pin_setImage(from: m.image.compress_small_image(width: 80*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            level_title_label.text = m.name
            
            
        }
    }
    
}



extension ST_KnighthoodLevelCell{
    
    func transformToLarge(){
        
        UIView.animate(withDuration: 0.2) {
            
            self.transform = CGAffineTransform(scaleX: 1.6, y: 1.6)
            
            self.icon_back_image_view.alpha = 1
            self.icon_back_image_view.play()
        }

        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.impactOccurred()
        
    }
    
    func transformToStandard(){
        
        UIView.animate(withDuration: 0.2) {
            
            self.transform = .identity
            self.icon_back_image_view.alpha = 0
            self.icon_back_image_view.pause()
        }
    }
    
}



//MARK: - Child
class ST_KnighthoodLevelCellChildBox: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(28)
            make.top.centerX.equalToSuperview()
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(11+28)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(36+28)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = .white
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = #colorLiteral(red: 0.5450980392, green: 0.5450980392, blue: 0.5607843137, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    

    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    var model:ST_KnighthoodLevelWidget?{
        didSet{
            
            guard let m = model else { return  }
            
            cover_image_view.pin_setImage(from: m.url.compress_small_image(width: 28*3).url)
            
            titleLabel.text = m.title
            
            content_label.text = m.subtitle
            
            self.alpha = m.isBuilding ? 0.5 : 1
        }
    }
}



//MARK: -
class ST_KnighthoodLevelModel  {

    var gapCell = false
    var checked : Bool
    var desc : String
    var endCoin : Int
    var id : Int
    var image : String
    var name : String
    var startCoin : Int
    var widget : [ST_KnighthoodLevelWidget]

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        checked = json["checked"].boolValue
        desc = json["desc"].stringValue
        endCoin = json["end_coin"].intValue
        id = json["id"].intValue
        image = json["image"].stringValue
        name = json["name"].stringValue
        startCoin = json["start_coin"].intValue
        
        widget = json["widget"].arrayValue.map({ ST_KnighthoodLevelWidget(fromJson: $0) })
        
        if widget.count > 0{
            let b = ST_KnighthoodLevelWidget(fromJson: JSON.null)
            
            b.isBuilding = true
            b.title = "施工中"
            b.subtitle = "更多特权"
            b.url = "http://btstatic.jingniaoapps.com/lc43b5w1630327623"
            
            widget.append(b)
        }
        
    }
}


class ST_KnighthoodLevelWidget {
    
    var id : Int
    var nobilityId : Int
    var sort : Int
    var subtitle : String
    var title : String
    var url : String
    var isBuilding : Bool = false

    init(fromJson json: JSON){
        
        id = json["id"].intValue
        nobilityId = json["nobility_id"].intValue
        sort = json["sort"].intValue
        subtitle = json["subtitle"].stringValue
        title = json["title"].stringValue
        url = json["url"].stringValue
    }
}
